//
//  XKPresentationManager.swift
//  WeiboSwift
//
//  Created by xiaokui wu on 9/11/16.
//  Copyright © 2016 wxk. All rights reserved.
//

import UIKit

let XKPresentationManagerDidPresentedController = "XKPresentationManagerDidPresentedController"
let XKPresentationManagerDidDismissedController = "XKPresentationManagerDidDismissedController"



class XKPresentationManager: NSObject,
UIViewControllerTransitioningDelegate,
UIViewControllerAnimatedTransitioning {
    
    
    private var isPresent = false
    var presentedViewFrame = CGRectZero
    
    
    // MARK: - UIViewControllerTransitioningDelegate
    // The custom presentation controller for managing the modal presentation.
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?
    {
        let pc = XKPresentationController(presentedViewController: presented, presentingViewController: presenting)
        pc.presentedViewFrame = presentedViewFrame
        return pc
    }
    
    // Asks your delegate for the transition animator object to use when presenting a view controller.
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isPresent = true
        // 发状态改变的通知
        NSNotificationCenter.defaultCenter().postNotificationName(XKPresentationManagerDidPresentedController, object: self)
        return self
    }
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        NSNotificationCenter.defaultCenter().postNotificationName(XKPresentationManagerDidDismissedController, object: self)
        isPresent = false
        return self
    }
    
    
    // MARK: - UIViewControllerAnimatedTransitioning

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval
    {
        return 0.5
    }
    
    // 专门管理modal如何展现和消失的，所有动画都需要自己实现，包括把需要展现的View添加到容器View上。也就是containerView中
    // 所有动画需要的东西都保存在上下文上，通过transitionContext 获得
    func animateTransition(transitionContext: UIViewControllerContextTransitioning)
    {
        
        // let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        if isPresent {
            willPresentedController(transitionContext)
        }else{
            willDismissedController(transitionContext)
        }
    }
    
    // 展现动画
    func willPresentedController(transitionContext: UIViewControllerContextTransitioning)
    {
        // 1. 获取需要弹出的视图
        if let toView = transitionContext.viewForKey(UITransitionContextToViewKey){
            transitionContext.containerView()?.addSubview(toView)
            
            toView.transform = CGAffineTransformMakeScale(1.0, 0.0)
            toView.layer.anchorPoint = CGPointMake(0.5, 0.0)
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
                toView.transform = CGAffineTransformIdentity
                }, completion: { (_) in
                    transitionContext.completeTransition(true)// 执行完转场动画后一定要调用这句
            })
        }
    }
    // 消失动画
    func willDismissedController(transitionContext: UIViewControllerContextTransitioning)
    {
        // 1. 获取需要消失的视图
        if let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey){
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
                fromView.transform = CGAffineTransformMakeScale(1.0, 0.00001)
                }, completion: { (_) in
                    transitionContext.completeTransition(true)// 执行完转场动画后一定要调用这句
            })
        }

    }

}
